package fei.yuzhi.com.loveflowers.fragment.mefragment.login;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bmob.v3.Bmob;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.PlatformDb;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import fei.yuzhi.com.loveflowers.R;
import fei.yuzhi.com.loveflowers.base.BaseActivity;
import fei.yuzhi.com.loveflowers.fragment.mefragment.login.forgetPass.ForgetPassActivity;
import fei.yuzhi.com.loveflowers.fragment.mefragment.login.register.RegisterActivity;
import fei.yuzhi.com.loveflowers.fragment.mefragment.login.register.UserBean;
import fei.yuzhi.com.loveflowers.utils.ActivityStackManagerUtils;

public class LoginActivity extends BaseActivity implements LoginContract.ViewLogin {

    @BindView(R.id.iv_login_back)
    ImageView ivLoginBack;
    @BindView(R.id.iv_login_name_backg)
    ImageView ivLoginNameBackg;
    @BindView(R.id.et_login_name)
    EditText etLoginName;
    @BindView(R.id.iv_login_pass_backg)
    ImageView ivLoginPassBackg;
    @BindView(R.id.et_login_pass)
    EditText etLoginPass;
    @BindView(R.id.iv_login_denglu)
    ImageView ivLoginDenglu;
    @BindView(R.id.iv_login_zhuce)
    ImageView ivLoginZhuce;
    @BindView(R.id.tv_login_forget)
    TextView tvLoginForget;
    @BindView(R.id.iv_login_threeL_wx)
    ImageView ivLoginThreeLWx;
    @BindView(R.id.iv_login_threeL_xinlang)
    ImageView ivLoginThreeLXinlang;
    @BindView(R.id.iv_login_threeL_qq)
    ImageView ivLoginThreeLQq;
    private LoginPresenter presenter;
    private boolean bl=false;
    public static final int LOGINJUMP=3;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityStackManagerUtils.getInstance().addActivity(this);
        Bmob.initialize(this, "7c6d1a4bf3aafd9f814667d6ad3cf6bb");
        presenter = new LoginPresenter(this);
        ShareSDK.initSDK(this);//初始化sharedsdk
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void onVerticalSucess(String msg, UserBean user) {
        //Toast.makeText(LoginActivity.this,msg, Toast.LENGTH_SHORT).show();
        EventBus.getDefault().post(user.getName());
        finish();
    }

    @Override
    public void onVerticalFail(String msg) {
        Toast.makeText(LoginActivity.this, msg+",无此用户", Toast.LENGTH_SHORT).show();
    }

    @OnClick({R.id.iv_login_back, R.id.iv_login_denglu, R.id.iv_login_zhuce, R.id.tv_login_forget, R.id.iv_login_threeL_wx, R.id.iv_login_threeL_xinlang, R.id.iv_login_threeL_qq})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_login_back:
                finish();
                break;
            case R.id.iv_login_denglu:
                denglu();

                break;
            case R.id.iv_login_zhuce:
                Intent intent=new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);

                break;
            case R.id.tv_login_forget:
                Intent intentForget=new Intent(LoginActivity.this, ForgetPassActivity.class);
                startActivity(intentForget);

                break;
            case R.id.iv_login_threeL_wx:
                break;
            case R.id.iv_login_threeL_xinlang:
               xinlangLogin(SinaWeibo.NAME);
                break;
            case R.id.iv_login_threeL_qq:
                xinlangLogin(QQ.NAME);
                break;
        }
    }

    private void xinlangLogin(String name) {
         bl=false;
        Platform platform= ShareSDK.getPlatform(name);
        if(platform.isAuthValid()) {
            platform.removeAccount(true);
        }
        platform.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int action, HashMap<String, Object> hashMap) {
                Log.i("tag","授权成功");
                //判断是否获取到用户信息
                if(action==platform.ACTION_USER_INFOR){
                    //获取数据平台数据
                    PlatformDb platformDb=platform.getDb();
                    // db对象获取用户数据
                    platformDb.getToken();//登录令牌
                    platformDb.getUserIcon();//获取用户图片
                    String userId=platformDb.getUserId();//获取当前第三方登录的用户id
                    String userName=platformDb.getUserName();//获取第三方用户名称
                    Log.i("tag","UserId="+userId+",UserName="+userName);

                    bl=true;
                    EventBus.getDefault().post(userName);
                }
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                Log.i("tag","登录失败!"+throwable);
            }

            @Override
            public void onCancel(Platform platform, int i) {
                Log.i("tag","登录取消!");
            }
        });
       // platform.authorize();
        platform.showUser(null);//执行登录 要数据不要功能 登录后回调里面用户数据
        platform.removeAccount(true);

    }

    private void denglu() {
        //判空
        if (RegisterActivity.isNull(etLoginName)&&RegisterActivity.isNull(etLoginPass)) {
                presenter.userLogin(etLoginName.getText().toString().trim(),etLoginPass.getText().toString());
        }else {
             Toast.makeText(LoginActivity.this, "用户名密码不能为空", Toast.LENGTH_SHORT).show();
        }
    }


}
